﻿#include "dialogspectrometer.h"
#include "ui_dialogspectrometer.h"
#include <QStyledItemDelegate>

#include "waitutils.h"
#include "devicemanager.h"
#include "widgetutils.h"

DialogSpectroMeter::DialogSpectroMeter(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSpectroMeter)
{
    ui->setupUi(this);

    _control=&SpectroMeterControl::Instance();
    InitDialog();
//     _plotWrap=new PlotWrap(ui->spectroMeter);
//     _plotWrap->InitCurve(3);

    _spectroListModel=new QStandardItemModel(this);

    // 设置水平表头标签
    _spectroListModel->setHorizontalHeaderLabels(QStringList() << "序列号" << "最小波长[nm]" << "最大波长[nm]"<<"波段宽度[nm]"<<"最小间隔[nm]"<<"最大间隔[nm]"<<"像元数");
    ui->tableView_spectroInfo->setModel(_spectroListModel);
    ui->tableView_spectroInfo->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView_spectroInfo->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView_spectroInfo->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView_spectroInfo->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView_spectroInfo->setAlternatingRowColors(true);
}

DialogSpectroMeter::~DialogSpectroMeter()
{
    delete ui;
}

void DialogSpectroMeter::InitDialog()
{

}

void DialogSpectroMeter::on_Btn_connect_clicked()
{
    QList<SpectroInfoTag> infoList=_control->GetSpectroMeterInfo();
    updataSpectroInfo(infoList);
}

void DialogSpectroMeter::updataSpectroInfo(QList<SpectroInfoTag> listSpectroInfo)
{
    qDebug()<<listSpectroInfo.count();
    _spectroListModel->setRowCount(0);

    for(int i = 0; i < listSpectroInfo.count(); i++) {
        _spectroListModel->setItem(i,0,new QStandardItem(QString("%1").arg(listSpectroInfo.at(i).serilID)));
        _spectroListModel->setItem(i,1,new QStandardItem(QString("%1").arg(listSpectroInfo.at(i).lambdaMin)));
        _spectroListModel->setItem(i,2,new QStandardItem(QString("%1").arg(listSpectroInfo.at(i).lambdaMax)));
        _spectroListModel->setItem(i,3,new QStandardItem(QString("%1").arg(listSpectroInfo.at(i).lambdaWidth)));
        _spectroListModel->setItem(i,4,new QStandardItem(QString("%1").arg(listSpectroInfo.at(i).lambdaIntervalMin)));
        _spectroListModel->setItem(i,5,new QStandardItem(QString("%1").arg(listSpectroInfo.at(i).lambdaIntervalMax)));
        _spectroListModel->setItem(i,6,new QStandardItem(QString("%1").arg(listSpectroInfo.at(i).pixelNumber)));
    }

    // 填充模型数据并设置居中对齐
    for (int row = 0; row < _spectroListModel->rowCount(); ++row) {
        for (int col = 0; col < _spectroListModel->columnCount(); ++col) {
            _spectroListModel->item(row,col)->setTextAlignment(Qt::AlignCenter);
        }
    }
    ui->tableView_spectroInfo->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

void DialogSpectroMeter::on_Btn_StartCollect_clicked()
{
//    WidgetUtils::Execute(sender(),[this]()
//    {
//        WaitUtils::SetRunning(true);
//        _plotWrap->InitAxis(0,30000);
//        for(int i=0;i<300;i++)
//        {
//            WaitUtils::WaitMsNoProgress(50);
//            _control->StartMeasure();
//            _plotWrap->ShowMult(_control->ReadData());

//        }
//    },true);
}

void DialogSpectroMeter::on_Btn_StopCollect_clicked()
{
    WaitUtils::Stop();
}
